package cn.wolfcode.luowowo.mgrsite.listener;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisVOService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 从mysql中查询数据同步到redis数据库中
 * redis 数据预热
 */
@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {

    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyStatisVOService strategyStatisVOService;

    //spring容器启动并且初始化完成之后 马上执行的操作
    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        System.out.println("------------------vo数据的初始化-start------------------");
        //mysql -- redis -- vo
        List<StrategyDetail> list = strategyDetailService.list();
        for (StrategyDetail detail : list) {
            //如果vo对象已经存在  则跳过不需要再进行初始化 防止数据被覆盖
            if(strategyStatisVOService.isStrategyVOExist(detail.getId())){
                continue;  //跳过
            }
            StrategyStatisVO vo = new StrategyStatisVO();
            //对象工具类  对象参数拷贝
            BeanUtils.copyProperties(detail,vo);
            vo.setDestId(detail.getDest().getId());
            vo.setDestName(detail.getDest().getName());
            vo.setStrategyId(detail.getId());
            strategyStatisVOService.setStrategyStatisVO(vo);

        }
        System.out.println("------------------vo数据的初始化-end------------------");
        System.out.println("");
        System.out.println("");
        System.out.println("------------------zset数据的初始化-start------------------");
        //热门攻略初始化

        for (StrategyDetail detail : list) {
            String zsetKey = RedisKeys.STRATEGY_HOT_ZSET.getPrefix();
            String voKey = RedisKeys.STRATEGY_STATIS_VO.join(detail.getId().toString());
            if(strategyStatisVOService.isHasZsetName(zsetKey,voKey)){
                continue;
            }
            int score = detail.getViewnum() + detail.getThumbsupnum();
            strategyStatisVOService.addScoreHot(detail.getId(),score);
        }

        //国内外攻略初始化
        for (StrategyDetail detail : list) {
            String zsetKey = null;
            if(detail.isIsabroad()){
                //国外
                zsetKey = RedisKeys.STRATEGY_OUTER_ZSET.getPrefix();
            }else{
                //国内
                zsetKey = RedisKeys.STRATEGY_ABBR_ZSET.getPrefix();
            }
            String voKey = RedisKeys.STRATEGY_STATIS_VO.join(detail.getId().toString());
            if(strategyStatisVOService.isHasZsetName(zsetKey,voKey)){
                continue;
            }
            int score = detail.getViewnum() + detail.getThumbsupnum();
            strategyStatisVOService.addScoreForeignOrChina(detail.getId(),score);
        }
        System.out.println("------------------zset数据的初始化-end------------------");
    }
}
